home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Unix / Shells / zsh / Source / src / cond.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-07  |  4.2 KB  |  192 lines

  1.  
  2. /*
  3.  *
  4.  * cond.c - evaluate conditional expressions
  5.  *
  6.  * This file is part of zsh, the Z shell.
  7.  *
  8.  * This software is Copyright 1992 by Paul Falstad
  9.  *
  10.  * Permission is hereby granted to copy, reproduce, redistribute or otherwise
  11.  * use this software as long as: there is no monetary profit gained
  12.  * specifically from the use or reproduction of this software, it is not
  13.  * sold, rented, traded or otherwise marketed, and this copyright notice is
  14.  * included prominently in any copy made.
  15.  *
  16.  * The author make no claims as to the fitness or correctness of this software
  17.  * for any use whatsoever, and it is provided as is. Any use of this software
  18.  * is at the user's own risk.
  19.  *
  20.  */
  21.  
  22. #include "zsh.h"
  23.  
  24. int evalcond(c)            /**/
  25. Cond c;
  26. {
  27.     struct stat *st;
  28.  
  29.     switch (c->type) {
  30.     case COND_NOT:
  31.     return !evalcond(c->left);
  32.     case COND_AND:
  33.     return evalcond(c->left) && evalcond(c->right);
  34.     case COND_OR:
  35.     return evalcond(c->left) || evalcond(c->right);
  36.     }
  37.     singsub((char **)&c->left);
  38.     untokenize(c->left);
  39.     if (c->right) {
  40.     singsub((char **)&c->right);
  41.     if (c->type != COND_STREQ && c->type != COND_STRNEQ)
  42.         untokenize(c->right);
  43.     }
  44.     switch (c->type) {
  45.     case COND_STREQ:
  46.     return matchpat(c->left, c->right);
  47.     case COND_STRNEQ:
  48.     return !matchpat(c->left, c->right);
  49.     case COND_STRLT:
  50.     return strcmp(c->left, c->right) < 0;
  51.     case COND_STRGTR:
  52.     return strcmp(c->left, c->right) > 0;
  53.     case 'e':
  54.     case 'a':
  55.     return (doaccess(c->left, F_OK));
  56.     case 'b':
  57.     return (S_ISBLK(dostat(c->left)));
  58.     case 'c':
  59.     return (S_ISCHR(dostat(c->left)));
  60.     case 'd':
  61.     return (S_ISDIR(dostat(c->left)));
  62.     case 'f':
  63.     return (S_ISREG(dostat(c->left)));
  64.     case 'g':
  65.     return (!!(dostat(c->left) & S_ISGID));
  66.     case 'k':
  67.     return (!!(dostat(c->left) & S_ISVTX));
  68.     case 'n':
  69.     return (!!strlen(c->left));
  70.     case 'o':
  71.     return (optison(c->left));
  72.     case 'p':
  73.     return (S_ISFIFO(dostat(c->left)));
  74.     case 'r':
  75.     return (doaccess(c->left, R_OK));
  76.     case 's':
  77.     return ((st = getstat(c->left)) && !!(st->st_size));
  78.     case 'S':
  79.     return (S_ISSOCK(dostat(c->left)));
  80.     case 'u':
  81.     return (!!(dostat(c->left) & S_ISUID));
  82.     case 'w':
  83.     return (doaccess(c->left, W_OK));
  84.     case 'x':
  85.     return (doaccess(c->left, X_OK));
  86.     case 'z':
  87.     return (!strlen(c->left));
  88.     case 'h':
  89.     case 'L':
  90.     return (S_ISLNK(dolstat(c->left)));
  91.     case 'O':
  92.     return ((st = getstat(c->left)) && st->st_uid == geteuid());
  93.     case 'G':
  94.     return ((st = getstat(c->left)) && st->st_gid == getegid());
  95.     case 't':
  96.     return isatty(matheval(c->left));
  97.     case COND_EQ:
  98.     return matheval(c->left) == matheval(c->right);
  99.     case COND_NE:
  100.     return matheval(c->left) != matheval(c->right);
  101.     case COND_LT:
  102.     return matheval(c->left) < matheval(c->right);
  103.     case COND_GT:
  104.     return matheval(c->left) > matheval(c->right);
  105.     case COND_LE:
  106.     return matheval(c->left) <= matheval(c->right);
  107.     case COND_GE:
  108.     return matheval(c->left) >= matheval(c->right);
  109.     case COND_NT:
  110.     case COND_OT:
  111.     {
  112.         time_t a;
  113.  
  114.         if (!(st = getstat(c->left)))
  115.         return 0;
  116.         a = st->st_mtime;
  117.         if (!(st = getstat(c->right)))
  118.         return 0;
  119.         return (c->type == COND_NT) ? a > st->st_mtime : a < st->st_mtime;
  120.     }
  121.     case COND_EF:
  122.     {
  123.         dev_t d;
  124.         ino_t i;
  125.  
  126.         if (!(st = getstat(c->left)))
  127.         return 0;
  128.         d = st->st_dev;
  129.         i = st->st_ino;
  130.         if (!(st = getstat(c->right)))
  131.         return 0;
  132.         return d == st->st_dev && i == st->st_ino;
  133.     }
  134.     default:
  135.     zerr("bad cond structure", NULL, 0);
  136.     }
  137.     return 0;
  138. }
  139.  
  140. int doaccess(s, c)        /**/
  141. char *s;
  142. int c;
  143. {
  144.     return !access(s, c);
  145. }
  146.  
  147. static struct stat st;
  148.  
  149. struct stat *getstat(s)        /**/
  150. char *s;
  151. {
  152.     if (!strncmp(s, "/dev/fd/", 8)) {
  153.     if (fstat(atoi(s + 8), &st))
  154.         return NULL;
  155.     } else if (stat(s, &st))
  156.     return NULL;
  157.     return &st;
  158. }
  159.  
  160. unsigned short dostat(s)    /**/
  161. char *s;
  162. {
  163.     struct stat *statp;
  164.  
  165.     if (!(statp = getstat(s)))
  166.     return 0;
  167.     return statp->st_mode;
  168. }
  169.  
  170. /* pem@aaii.oz; needed since dostat now uses "stat" */
  171.  
  172. unsigned short dolstat(s)    /**/
  173. char *s;
  174. {
  175.     if (lstat(s, &st) < 0)
  176.     return 0;
  177.     return st.st_mode;
  178. }
  179.  
  180. int optison(s)            /**/
  181. char *s;
  182. {
  183.     int i;
  184.  
  185.     if (strlen(s) == 1)
  186.     return opts[(int)*s];
  187.     if ((i = optlookup(s)) != -1)
  188.     return opts[i];
  189.     zerr("no such option: %s", s, 0);
  190.     return 0;
  191. }
  192.